package com.graduation.rescuematerial.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/12 14:00
 * @Version:
 * @Description:swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 创建API的基本信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("应急救援物资管理系统")
                .description("应急救援物资管理系统的API说明")
                .termsOfServiceUrl("http://localhost:8080")
                .version("1.1.0")
                .contact(new Contact("CaiShuangLian", "http://localhost:8080/", "18090202385@163.com"))
                .build();
    }

    @Bean
    public Docket createRestApi() {
        List<ResponseMessage> responseMessages = new ArrayList<>();

        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .produces(Collections.singleton("application/json"))
                .consumes(Collections.singleton("application/json"))
                .protocols(Collections.singleton("http"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.graduation.rescuematerial.controller"))
                .paths(PathSelectors.any())
                .build()
//                .globalOperationParameters(operationParameters())
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages);
    }

    private List<Parameter> operationParameters() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();

        pars.add(tokenPar
                .name("Token")
                .description("Token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build()
        );
        pars.add(tokenPar
                .name("userId")
                .description("userId")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());
        pars.add(tokenPar.build());
        return pars;
    }
}
